package com.blockmeta.bbs.immodule.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CommonPopup extends Dialog
{
	private static List<CommonPopup> mList = new ArrayList<>();

	private static void addCacheFDTPopup(CommonPopup popup)
	{
		mList.add(popup);
	}

	private static void removeCacheFDTPopup(CommonPopup popup)
	{
		mList.remove(popup);
	}

	public static void closeCachePopups()
	{
		if (mList.size() > 0)
		{
			ArrayList<CommonPopup> list = new ArrayList<>(mList);

			for (CommonPopup popup : list)
			{
				if (popup.isShowing())
				{
					popup.dismiss();
				}
			}

			mList.clear();
		}
	}

	public interface onDismissListener
	{
		public void onDismiss();
	}

	private onDismissListener m_listenerOnDismiss;

	private OnDismissListener m_onDismissListener;
	
	public int m_key;
	private View m_view;
	private boolean m_modal = false;

	private boolean m_closeInViewChange;

	private Handler m_Handler;

	public CommonPopup(Context context, int theme)
	{
		this(context, theme, true);
	}

	public CommonPopup(Context context, int theme, boolean closeInViewChange)
	{
		super(context, theme);

		m_Handler = new InternalHandler();

		super.setOnDismissListener(new OnDismissListener()
		{
			@Override
			public void onDismiss(DialogInterface dialogInterface)
			{
				if (m_onDismissListener != null)
				{
					m_onDismissListener.onDismiss(dialogInterface);
				}

				if (m_listenerOnDismiss != null)
				{
					m_listenerOnDismiss.onDismiss();
				}

				if (m_closeInViewChange)
				{
					removeCacheFDTPopup(CommonPopup.this);
				}

				m_Handler.removeCallbacksAndMessages(null);
			}
		});

		m_closeInViewChange = closeInViewChange;
	}

	public void show()
	{
		super.show();
		setCanceledOnTouchOutside(true);

		if (m_closeInViewChange)
		{
			addCacheFDTPopup(this);
		}
	}


	public void setOnDismissListener(onDismissListener listener)
	{
		m_listenerOnDismiss = listener;
	}

	public void setOnDismissListener(OnDismissListener listener)
	{
		m_onDismissListener = listener;
	}

	public void setContentView(View view, FrameLayout.LayoutParams params, int gravity)
	{
		super.setContentView(view, params);

		this.m_view = getWindow().getDecorView();
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.gravity = gravity;
		lp.x = params.leftMargin;
		lp.y = params.topMargin;
		lp.width = params.width;
		lp.height = params.height;
		getWindow().setAttributes(lp);

		params.setMargins(0, 0, 0, 0);
		view.setLayoutParams(params);
	}

	public boolean onTouchEvent(MotionEvent event)
	{
		if (this.m_modal)
		{
			return super.onTouchEvent(event);
		}

		if (this.m_view == null)
		{
			return super.onTouchEvent(event);
		}

		Rect outRect = new Rect();
		this.m_view.getHitRect(outRect);
		if (outRect.contains((int) event.getX(), (int) event.getY()))
		{
			return super.onTouchEvent(event);
		}
		outRect = null;
		dismiss();
		return true;
	}

	public void setModal()
	{
		this.m_modal = true;
	}


	public void runOnUiThread(Runnable runnable)
	{
		if (Looper.myLooper() != Looper.getMainLooper())
		{
			m_Handler.post(runnable);
		}
		else
		{
			runnable.run();
		}
	}

	private List<Runnable> m_DelayRunnables = Collections.synchronizedList
			(new ArrayList<Runnable>());

	public void runOnUiThreadDelay(Runnable runnable)
	{
		m_DelayRunnables.add(runnable);

		if (! m_Handler.hasMessages(0x01))
		{
			m_Handler.sendEmptyMessageDelayed(0x01, 300);
		}
	}

	public void runOnUiThreadDelay(int what, Runnable runnable)
	{
		if (! m_Handler.hasMessages(what))
		{
			m_DelayRunnables.add(runnable);

			m_Handler.sendEmptyMessageDelayed(what, 300);
		}
	}

	public void runOnUiThreadDelay(int what, Runnable runnable, long delay)
	{
		if (! m_Handler.hasMessages(what))
		{
			m_DelayRunnables.add(runnable);

			m_Handler.sendEmptyMessageDelayed(what, delay);
		}
	}

	private class InternalHandler extends Handler
	{
		@Override
		public void handleMessage(Message msg)
		{
			List<Runnable> list = new ArrayList<>(m_DelayRunnables);
			m_DelayRunnables.clear();

			int size = list.size();
			for (int i = 0; i < size; i++)
			{
				Runnable runnable = list.get(i);
				runnable.run();
			}
		}
	}
}
